package com.example.demo.controller;

import com.example.demo.entity.CodeAssociation;
import com.example.demo.entity.CodeHistory;
import com.example.demo.service.CodeAssociationService;
import com.example.demo.util.GetMessage;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/api/association")
@RequiredArgsConstructor
@Component
public class CodeAssociationController {
    private final CodeAssociationService codeAssociationService;

    @CrossOrigin(origins ="*" ,maxAge = 3600)
    @GetMapping("/select")
    public GetMessage select (String code){

        CodeAssociation list=codeAssociationService.select(code);
        return new GetMessage("成功","200",list);
    }
    @CrossOrigin(origins ="*" ,maxAge = 3600)
    @GetMapping("/selectbyinfo")
    public GetMessage selectbyinfo (String code,String userId,String trainingId,String appId){

        List<CodeAssociation> list=codeAssociationService.selectbyinfo(code,userId,trainingId,appId);
        return new GetMessage("成功","200",list);
    }
    @CrossOrigin(origins ="*" ,maxAge = 3600)
    @GetMapping("/ismatch")
    public GetMessage ismatch (String userId,String trainingId ,String codeList ){
        //String userId=token;
        String[] codeLists=codeList.split(",");
        List<CodeAssociation> list= codeAssociationService.selectByInfo(userId,"gree",trainingId);
        for (CodeAssociation c:list) {
            String[] codes=c.getAssociationcode().split(",");
            Set<String> stringSet = new HashSet<>(Arrays.asList(codes));
            if(stringSet.contains(codeLists[0])&&stringSet.contains(codeLists[1])){
                return new GetMessage("成功","200",true);
            }
        }
        return new GetMessage("成功","200",false);
    }
}
